/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OpenAddPriceFrame.java
 *
 * Created on 16.02.2011, 12:44:59
 */

package docview;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import tableprocessor.DBSupplier;
import tableprocessor.DBSupplierDAO;

/**
 *
 * @author gleb
 */
public class OpenAddPriceFrame extends javax.swing.JFrame {

    /** Creates new form OpenAddPriceFrame */
    public OpenAddPriceFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        choosePanel = new javax.swing.JPanel();
        supplierComboBox = new javax.swing.JComboBox();
        addCheckBox = new javax.swing.JCheckBox();
        propPanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        adressLabel = new javax.swing.JLabel();
        adressField = new javax.swing.JTextField();
        phoneLabel = new javax.swing.JLabel();
        phoneField = new javax.swing.JTextField();
        regionLabel = new javax.swing.JLabel();
        regionField = new javax.swing.JTextField();
        buttonPanel = new javax.swing.JPanel();
        filePanel = new javax.swing.JPanel();
        chooseFileBtn = new javax.swing.JButton();
        chooseFileField = new javax.swing.JTextField();
        actPanel = new javax.swing.JPanel();
        nextBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();

        setName("Form"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridLayout(3, 1));

        choosePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        choosePanel.setName("choosePanel"); // NOI18N
        choosePanel.setLayout(new java.awt.GridLayout(2, 1));

        supplierComboBox.setName("supplierComboBox"); // NOI18N
        choosePanel.add(supplierComboBox);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(docview.DocViewApp.class).getContext().getResourceMap(OpenAddPriceFrame.class);
        addCheckBox.setText(resourceMap.getString("addCheckBox.text")); // NOI18N
        addCheckBox.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        addCheckBox.setName("addCheckBox"); // NOI18N
        addCheckBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                addCheckBoxMouseClicked(evt);
            }
        });
        choosePanel.add(addCheckBox);

        getContentPane().add(choosePanel);

        propPanel.setName("propPanel"); // NOI18N
        propPanel.setLayout(new java.awt.GridLayout(4, 2));

        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setToolTipText(resourceMap.getString("nameLabel.toolTipText")); // NOI18N
        nameLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        nameLabel.setName("nameLabel"); // NOI18N
        propPanel.add(nameLabel);

        nameField.setEnabled(false);
        nameField.setName("nameField"); // NOI18N
        propPanel.add(nameField);

        adressLabel.setText(resourceMap.getString("adressLabel.text")); // NOI18N
        adressLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        adressLabel.setName("adressLabel"); // NOI18N
        propPanel.add(adressLabel);

        adressField.setEnabled(false);
        adressField.setName("adressField"); // NOI18N
        propPanel.add(adressField);

        phoneLabel.setText(resourceMap.getString("phoneLabel.text")); // NOI18N
        phoneLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        phoneLabel.setName("phoneLabel"); // NOI18N
        propPanel.add(phoneLabel);

        phoneField.setEnabled(false);
        phoneField.setName("phoneField"); // NOI18N
        propPanel.add(phoneField);

        regionLabel.setText(resourceMap.getString("regionLabel.text")); // NOI18N
        regionLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        regionLabel.setName("regionLabel"); // NOI18N
        propPanel.add(regionLabel);

        regionField.setEnabled(false);
        regionField.setName("regionField"); // NOI18N
        propPanel.add(regionField);

        getContentPane().add(propPanel);

        buttonPanel.setName("buttonPanel"); // NOI18N
        buttonPanel.setLayout(new java.awt.GridLayout(2, 1));

        filePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        filePanel.setName("filePanel"); // NOI18N
        filePanel.setLayout(new java.awt.GridLayout(1, 2));

        chooseFileBtn.setText(resourceMap.getString("chooseFileBtn.text")); // NOI18N
        chooseFileBtn.setName("chooseFileBtn"); // NOI18N
        chooseFileBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                chooseFileBtnMouseClicked(evt);
            }
        });
        filePanel.add(chooseFileBtn);

        chooseFileField.setName("chooseFileField"); // NOI18N
        filePanel.add(chooseFileField);

        buttonPanel.add(filePanel);

        actPanel.setName("actPanel"); // NOI18N
        actPanel.setLayout(new java.awt.GridLayout(1, 2));

        nextBtn.setText(resourceMap.getString("nextBtn.text")); // NOI18N
        nextBtn.setName("nextBtn"); // NOI18N
        nextBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextBtnMouseClicked(evt);
            }
        });
        actPanel.add(nextBtn);

        cancelBtn.setText(resourceMap.getString("cancelBtn.text")); // NOI18N
        cancelBtn.setName("cancelBtn"); // NOI18N
        cancelBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelBtnMouseClicked(evt);
            }
        });
        actPanel.add(cancelBtn);

        buttonPanel.add(actPanel);

        getContentPane().add(buttonPanel);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public OpenAddPriceFrame(DocViewManager _manager)
    {
        docManager = _manager;
        initComponents();
    }
    private void addCheckBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addCheckBoxMouseClicked
        nameField.setEnabled(!nameField.isEnabled());
        adressField.setEnabled(!adressField.isEnabled());
        phoneField.setEnabled(!phoneField.isEnabled());
        regionField.setEnabled(!regionField.isEnabled());
    }//GEN-LAST:event_addCheckBoxMouseClicked

    private void cancelBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelBtnMouseClicked
       this.setVisible(false);
    }//GEN-LAST:event_cancelBtnMouseClicked

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        siter = DBSupplierDAO.getSupplierList();
        while(siter.hasNext())
        {
           supplierComboBox.addItem(siter.next().getName());
        }
    }//GEN-LAST:event_formComponentShown

    private void nextBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextBtnMouseClicked
        boolean rez = true;
        if (addCheckBox.isSelected())
        {
            if (!nameField.getText().trim().isEmpty())
            {
                DBSupplierDAO.save(nameField.getText(),
                            adressField.getText(),
                            phoneField.getText(),
                            regionField.getText());
                supplierComboBox.removeAllItems();
                siter = DBSupplierDAO.getSupplierList();
                while(siter.hasNext())
                {
                    supplierComboBox.addItem(siter.next().getName());
                }
                rez = true;
            }
            else
            {
                rez = false;
                JOptionPane.showMessageDialog(null, " Поле \"Название\" не может быть пустым");
            }
        }
        if (rez && !chooseFileField.getText().isEmpty())
        {
            docManager.openFile(file);
             docManager.createNewView(new ViewNewDoc(docManager));
             docFrame = new JFrame(file.getName());
             docFrame.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent evt) {
                     docFrame.dispose();
                 }
             });
            docFrame.getContentPane().add(docManager.showView());
            docFrame.pack();
            docFrame.setVisible(true);
            this.setVisible(false);
        }
        if (chooseFileField.getText().isEmpty())
        {
            if (JOptionPane.showConfirmDialog(null, "Создать новый прайс?") == 0 )
            {
                    docManager.createEmptyTable();
                    docManager.createNewView(new ViewNewDoc(docManager)) ;
                    //ViewNewDoc vDoc = new ViewNewDoc();
                    docFrame = new JFrame("Новый прайс");
                    docFrame.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent evt) {docFrame.dispose();}});
                    docFrame.getContentPane().add(docManager.showView());
                    docFrame.pack();
                    docFrame.setVisible(true);
                    this.setVisible(false);
            }
        }
    }//GEN-LAST:event_nextBtnMouseClicked

    private void chooseFileBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_chooseFileBtnMouseClicked
       JFileChooser fc = new JFileChooser();
        int returnCode = fc.showDialog(null,"Открыть файл");
        if (returnCode == JFileChooser.APPROVE_OPTION)
        {
            file = fc.getSelectedFile();
            chooseFileField.setText(file.getAbsolutePath());
            
        }
    }//GEN-LAST:event_chooseFileBtnMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OpenAddPriceFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actPanel;
    private javax.swing.JCheckBox addCheckBox;
    private javax.swing.JTextField adressField;
    private javax.swing.JLabel adressLabel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JButton chooseFileBtn;
    private javax.swing.JTextField chooseFileField;
    private javax.swing.JPanel choosePanel;
    private javax.swing.JPanel filePanel;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton nextBtn;
    private javax.swing.JTextField phoneField;
    private javax.swing.JLabel phoneLabel;
    private javax.swing.JPanel propPanel;
    private javax.swing.JTextField regionField;
    private javax.swing.JLabel regionLabel;
    private javax.swing.JComboBox supplierComboBox;
    // End of variables declaration//GEN-END:variables
    private Iterator<DBSupplier> siter;
    private DocViewManager docManager;
    private JFrame docFrame;
    private File file;
}
